{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7534d20a",
   "metadata": {},
   "source": [
    "## 人脸检测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "13dad409",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "api_key = \"dAyJdlVlkTKc2PHaDc_Qoc-5NXTWZEx-\"\n",
    "api_secret = \"ue8dzp1KefHMSLYWXP3ExZLKELfoBOEp\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0d658c79",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸检测url\n",
    "detectUrl = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1edaf3ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"image_url\": \"https://cdn.pixabay.com/photo/2017/06/18/18/26/holi-2416686_1280.jpg\"\n",
    "}\n",
    "\n",
    "r = requests.post(detectUrl, payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1cf30f95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2aad791a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868287,bff7fc1c-7fdb-4571-bd60-1f7daa7eac0f',\n",
       " 'time_used': 2984,\n",
       " 'faces': [{'face_token': '842f67ef2e49114bd34ad14389ff252b',\n",
       "   'face_rectangle': {'top': 501, 'left': 1199, 'width': 127, 'height': 127}},\n",
       "  {'face_token': '0147fd2cdc77b7f0a675938b03534bc4',\n",
       "   'face_rectangle': {'top': 316, 'left': 550, 'width': 127, 'height': 127}},\n",
       "  {'face_token': '0f6f15a9c73765c5421daa291f5071d4',\n",
       "   'face_rectangle': {'top': 295, 'left': 215, 'width': 95, 'height': 95}},\n",
       "  {'face_token': 'aff3fbc37da2348e5ae0b8b2900ba968',\n",
       "   'face_rectangle': {'top': 171, 'left': 1099, 'width': 92, 'height': 92}}],\n",
       " 'image_id': 'kXgYO0CaeWJezfjeJayRwA==',\n",
       " 'face_num': 4}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9327cacc",
   "metadata": {},
   "source": [
    "## 人脸分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f21e6118",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'842f67ef2e49114bd34ad14389ff252b'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f4aa53d9",
   "metadata": {},
   "outputs": [],
   "source": [
    "analyzeUrl = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6583a5a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"face_tokens\": face_token,\n",
    "#     \"return_landmark\": 1,\n",
    "    \"return_attributes\": \"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\"\n",
    "}\n",
    "\n",
    "r = requests.post(analyzeUrl, payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "5a400fd5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "53571da1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 195,\n",
       " 'request_id': '1647868291,a2ca66da-2f91-4cf2-a3c6-5bb686851cf8',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.03,\n",
       "     'neutral': 0.178,\n",
       "     'disgust': 66.062,\n",
       "     'anger': 0.03,\n",
       "     'surprise': 32.924,\n",
       "     'fear': 0.03,\n",
       "     'happiness': 0.745},\n",
       "    'beauty': {'female_score': 61.169, 'male_score': 67.208},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 58},\n",
       "    'mouthstatus': {'close': 74.962,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 12.857,\n",
       "     'other_occlusion': 12.181},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 47.882,\n",
       "     'stain': 9.033,\n",
       "     'acne': 6.024,\n",
       "     'health': 0.154},\n",
       "    'headpose': {'yaw_angle': 98.02698,\n",
       "     'pitch_angle': 2.0978448,\n",
       "     'roll_angle': 3.8317199},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 12.419},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 12.419},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 12.419}},\n",
       "    'smile': {'threshold': 50.0, 'value': 41.068},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 99.999,\n",
       "      'no_glass_eye_open': 0.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.062,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.392,\n",
       "      'no_glass_eye_open': 0.013,\n",
       "      'normal_glass_eye_close': 0.001,\n",
       "      'dark_glasses': 99.533}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 0.006},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.366,\n",
       "      'vector_z_component': 0.798,\n",
       "      'vector_x_component': -0.483,\n",
       "      'vector_y_component': 0.359,\n",
       "      'position_y_coordinate': 0.47},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.433,\n",
       "      'vector_z_component': 0.946,\n",
       "      'vector_x_component': -0.244,\n",
       "      'vector_y_component': 0.214,\n",
       "      'position_y_coordinate': 0.326}}},\n",
       "   'face_rectangle': {'width': 127, 'top': 501, 'left': 1199, 'height': 127},\n",
       "   'face_token': '842f67ef2e49114bd34ad14389ff252b'}]}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "89a756b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>842f67ef2e49114bd34ad14389ff252b</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>0.178</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>66.062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>32.924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.745</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>61.169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>67.208</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>58</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>74.962</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>12.857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>12.181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>47.882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>9.033</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>6.024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>0.154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>98.02698</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>2.097845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>3.83172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>12.419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>12.419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>12.419</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>41.068</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>99.999</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.392</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>0.013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>99.533</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>0.006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.366</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.798</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>0.359</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.433</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.946</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.244</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>0.214</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.326</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>501</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>1199</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>127</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          842f67ef2e49114bd34ad14389ff252b\n",
       "attributes.emotion.sadness                                                      0.03\n",
       "attributes.emotion.neutral                                                     0.178\n",
       "attributes.emotion.disgust                                                    66.062\n",
       "attributes.emotion.anger                                                        0.03\n",
       "attributes.emotion.surprise                                                   32.924\n",
       "attributes.emotion.fear                                                         0.03\n",
       "attributes.emotion.happiness                                                   0.745\n",
       "attributes.beauty.female_score                                                61.169\n",
       "attributes.beauty.male_score                                                  67.208\n",
       "attributes.gender.value                                                         Male\n",
       "attributes.age.value                                                              58\n",
       "attributes.mouthstatus.close                                                  74.962\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                   12.857\n",
       "attributes.mouthstatus.other_occlusion                                        12.181\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                             47.882\n",
       "attributes.skinstatus.stain                                                    9.033\n",
       "attributes.skinstatus.acne                                                     6.024\n",
       "attributes.skinstatus.health                                                   0.154\n",
       "attributes.headpose.yaw_angle                                               98.02698\n",
       "attributes.headpose.pitch_angle                                             2.097845\n",
       "attributes.headpose.roll_angle                                               3.83172\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                12.419\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                              12.419\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                            12.419\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                        41.068\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                99.999\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.062\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                0.392\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             0.013\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.001\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                            99.533\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                   0.006\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.366\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.798\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.483\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                             0.359\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                              0.47\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.433\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.946\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                            -0.244\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                             0.214\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.326\n",
       "face_rectangle.width                                                             127\n",
       "face_rectangle.top                                                               501\n",
       "face_rectangle.left                                                             1199\n",
       "face_rectangle.height                                                            127"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# 实现数据表格化\n",
    "# pd.json_normalize(r.json())\n",
    "pd.json_normalize(r.json()[\"faces\"]).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6851816c",
   "metadata": {},
   "source": [
    "## 封装函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c24fc658",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 封装人脸检测api\n",
    "def face_detect(api_key,api_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload={\n",
    "        'api_key': api_key,\n",
    "        'api_secret': api_secret,\n",
    "        'image_url': image_url,\n",
    "        'return_landmark': landmark,\n",
    "        'return_attributes': attributes,\n",
    "        'face_rectangle': face_rectangle,\n",
    "        'beauty_score_min': beauty_score_min,\n",
    "        'beauty_score_max': beauty_score_max   \n",
    "    }\n",
    "    r = requests.post(url,payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b78ab817",
   "metadata": {},
   "source": [
    "## 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "156fa012",
   "metadata": {},
   "outputs": [],
   "source": [
    "compareUrl = \"https://api-cn.faceplusplus.com/facepp/v3/compare\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "20ccb3db",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868801,b4a4fabb-deff-4fa7-909a-baed8a64b5d2',\n",
       " 'time_used': 805,\n",
       " 'faces': [{'face_token': 'fa886c9bef6222d4273187f6cbd064db',\n",
       "   'face_rectangle': {'top': 149, 'left': 285, 'width': 132, 'height': 132}}],\n",
       " 'image_id': 'WI/B3dqIay6QRLKpT7QDHQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第一张图片token\n",
    "first = face_detect(api_key, api_secret, \"https://inews.gtimg.com/newsapp_bt/0/13626145381/1000\")\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8f64f2ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'fa886c9bef6222d4273187f6cbd064db'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face01_token = first[\"faces\"][0][\"face_token\"]\n",
    "face01_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "5d99c5fe",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868806,a65b53cc-bd1f-459d-8db9-116a0d7d0941',\n",
       " 'time_used': 198,\n",
       " 'faces': [{'face_token': 'f53dfb3893b3ebfdbfb49d4f7d729c47',\n",
       "   'face_rectangle': {'top': 172, 'left': 155, 'width': 222, 'height': 222}}],\n",
       " 'image_id': '981B6hLd7q+S9wBWwV8dUg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取第二张图片token\n",
    "second = face_detect(api_key, api_secret, \"https://n.sinaimg.cn/fashion/crawl/559/w550h809/20220315/a10f-0a3228d5b1bff8bfe566b91a2dc18d73.jpg\")\n",
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "6f1d88f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'f53dfb3893b3ebfdbfb49d4f7d729c47'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face02_token = second[\"faces\"][0][\"face_token\"]\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "fd31bd6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"face_token1\": face01_token,\n",
    "    \"face_token2\": face02_token\n",
    "}\n",
    "\n",
    "r = requests.post(compareUrl, payload)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5cabedcb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647868809,888fe464-927b-442a-bf98-216870e910c5',\n",
       " 'time_used': 406,\n",
       " 'confidence': 88.371,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a224f10",
   "metadata": {},
   "source": [
    "## 人脸集合\n",
    "- **创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。**\n",
    "    - display_name：人脸集合的名字\n",
    "    - outer_id：账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象\n",
    "    - user_data：自定义用户信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "1442ade4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3daacf6c5f257c4e8d7c5272b13a27f6',\n",
       " 'time_used': 177,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647869406,10c022fb-850f-49d3-bdd2-3a9644551585',\n",
       " 'outer_id': 'face002',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个人脸集合 FaceSet，用于存储人脸标识 face_token\n",
    "createUrl = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "display_name = \"人脸集合\"\n",
    "outer_id = \"face002\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"display_name\":display_name,\n",
    "    \"outer_id\":outer_id,\n",
    "}\n",
    "r = requests.post(createUrl, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "70a4d180",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 91,\n",
       " 'facesets': [{'faceset_token': '0387d9794143c1e290e34a7110138c66',\n",
       "   'outer_id': 'face09',\n",
       "   'display_name': '人脸集合',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'e550db127ad0b3ec90934058cbcaeba9',\n",
       "   'outer_id': 'face001',\n",
       "   'display_name': '人脸集合',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '3daacf6c5f257c4e8d7c5272b13a27f6',\n",
       "   'outer_id': 'face002',\n",
       "   'display_name': '人脸集合',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647869411,1a8d11df-81ca-4778-9683-239623719bdb'}"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取人脸库列表及信息\n",
    "getFaces_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "}\n",
    "\n",
    "r = requests.post(getFaces_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "5e3819e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'3daacf6c5f257c4e8d7c5272b13a27f6'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取 faceset_token\n",
    "faceset_token = r.json()[\"facesets\"][2][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b9bc931",
   "metadata": {},
   "source": [
    "## 添加人脸\n",
    "- **为一个已经创建的 FaceSet 添加人脸标识 face_token**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "e8152812",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数封装添加人脸\n",
    "def addFace(api_key,api_secret,faceset_token,face_tokens):\n",
    "    addFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "    payload = {\n",
    "        \"api_key\": api_key,\n",
    "        \"api_secret\": api_secret,\n",
    "        \"faceset_token\": faceset_token,     \n",
    "        \"face_tokens\": face_tokens}\n",
    "    \n",
    "    r = requests.post(addFace_url,params=payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "40937bde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3daacf6c5f257c4e8d7c5272b13a27f6',\n",
       " 'time_used': 711,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647869580,d4b4f0a0-d70d-45c4-a083-5d318de7d912',\n",
       " 'outer_id': 'face002',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "addFace(api_key, api_secret,faceset_token, face01_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f265ec5b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3daacf6c5f257c4e8d7c5272b13a27f6',\n",
       " 'time_used': 525,\n",
       " 'face_count': 2,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647869591,5ce7ef3b-9700-48c5-8e15-8089ad3d0062',\n",
       " 'outer_id': 'face002',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "addFace(api_key, api_secret,faceset_token, face02_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "6183de84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '3daacf6c5f257c4e8d7c5272b13a27f6',\n",
       " 'time_used': 552,\n",
       " 'face_count': 3,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1647869789,5764a40f-5847-449e-9e1d-1a8d2814c75f',\n",
       " 'outer_id': 'face002',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "addFace(api_key, api_secret,faceset_token, face_token)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c4374ec",
   "metadata": {},
   "source": [
    "## 人脸搜索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "e3e0015e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "searchUrl = \"https://api-cn.faceplusplus.com/facepp/v3/search\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"face_token\": face01_token,\n",
    "    \"faceset_token\": faceset_token,\n",
    "}\n",
    "\n",
    "r = requests.post(searchUrl, payload)\n",
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "0ed6e836",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647869842,6d88ee98-2b8a-4502-a9ef-446a9a19bf36',\n",
       " 'time_used': 456,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.389,\n",
       "   'user_id': '',\n",
       "   'face_token': 'fa886c9bef6222d4273187f6cbd064db'}]}"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1199630c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
